﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LiteExpress.Utilities
{
    public class CacheHelper
    {
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="Key"></param>
        public static void RemoveCache(string Key)
        {
            HttpRuntime.Cache.Remove(Key);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static T GetCache<T>(string Key)
        {
            var obj = HttpRuntime.Cache[Key];
            if (obj == null)
            {
                return default(T);
            }

            var result = (T)obj;
            return result;
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        public static void SetCache(string Key, object value, double expireHours)
        {
            //目前不存在缓存依赖，如果存在增加依赖对象
            HttpRuntime.Cache.Add(Key, value, null, DateTime.Now.AddHours(expireHours)
                , System.Web.Caching.Cache.NoSlidingExpiration
                , System.Web.Caching.CacheItemPriority.High
                , null);
        }
    }
}
